package com.szd.qiongyou20.community.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.szd.qiongyou20.R;
import com.szd.qiongyou20.community.adapter.ViewPager_Adapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SZD on 2016/11/25.
 */

public class Fragment_Community extends Fragment {

    private Context mContext;
    private FragmentManager mFragmentManager;
    private List<Fragment> mList;
    private String[] TabTitles;

    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mList = new ArrayList<>();
        mFragmentManager = getFragmentManager();
        TabTitles = getResources().getStringArray(R.array.community_titles);


    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_community, null);
        mTabLayout = (TabLayout) view.findViewById(R.id.tabLayout_community);
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager_community);
        mViewPager.setOffscreenPageLimit(2);

        // 初始化碎片
        fragmentList();

        ViewPager_Adapter adapter = new ViewPager_Adapter(mFragmentManager, mList, TabTitles);
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);

        return view;
    }

    /**
     * 初始化碎片的集合
     */
    private void fragmentList() {

        // 推荐
        Fragment_Recommend fragment_recommend = new Fragment_Recommend();
        mList.add(fragment_recommend);

        // 进入版面
        Fragment_Enter fragment_enter = new Fragment_Enter();
        mList.add(fragment_enter);
    }
}
